﻿using FrameworkCore.Controllers;
using Microsoft.AspNetCore.Mvc;
using FrameworkCore;
using AiBLSmartEdu.Module.Cust.API.DTOs;
using AiBLSmartEdu.Module.Cust.API.Repositories;
using Domain.Entities;
using FrameworkCore.Models;

namespace AiBLSmartEdu.Module.Cust.API.Controllers;

/// <summary>
/// 用户管理
/// </summary>
public partial class CustController : BaseModuleController
{
    private readonly CustomerRepository  _customerRepository;
    public CustController(
        CustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="filter">分页输入模型</param>
    /// <returns></returns>
    [HttpGet("Page")]
    public async Task<PageResult<CustomerOutputModel>> Pagination([FromQuery] CustomerPaginationFilter filter)
    {
       return await _customerRepository.PageAsync(filter);
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("Info")]
    public async Task<Result<CustomerOutputModel>> GetCustInfo()
    {
        var result = await _customerRepository.GetCustInfo();
        return await Result<CustomerOutputModel>.SucceedAsync(result);
    }

    /// <summary>
    /// 单个查询
    /// </summary>
    /// <param name="id">唯一标识</param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<Result<Customer>> First(int id)
    {
        var result = await _customerRepository.GetByIdAsync(id);
        return await Result<Customer>.SucceedAsync(result);
    }

    /// <summary>
    /// 创建用户
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Result> Create(CustomerCreateInputModel input)
    {
        await _customerRepository.CreateAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改用户邮箱
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPut("Email")]
    public async Task<Result> UpdateEmail(CustomerUpdateEmailInputModel input)
    {
        await _customerRepository.UpdateEmailAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改用户头像
    /// </summary>
    /// <param name="input">输入模型</param>
    /// <returns></returns>
    [HttpPut("Avatar")]
    public async Task<Result> UpdateAvatar(UpdateAvatarInputModel input)
    {
        await _customerRepository.UpdateAvatarAsync(input);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 批量删除用户
    /// </summary>
    /// <returns></returns>
    [HttpDelete("{ids}")]
    public async Task<Result> BulkDeletes(string ids)
    {
        await _customerRepository.DeleteAsync(ids);
        return await Result.SucceedAsync();
    }
}
